<!-- 师傅端个人中心的合同下载 -->
<template>
	<view class="warp">
		<scroll-view class="shop_item" scroll-y >
			<view class="htxzbox" v-for="item in listdata" :key="item.id" >
				<view class="htxztime word-omit">{{item.add_time}}</view>
				<view class="htxztitle">{{item.name}}</view>
				<view class="htxzbtn">
					<view class="btnlook" @click="look(item)" >在线查看</view>
					<view class="btndown" @click="downWord(item.url)">下载到本地</view>
				</view>
			</view>
		</scroll-view>

	</view>
</template>

<script>
	import {
		getWorkerContract
	} from '../../../api/api.js'
	export default {
		data() {
			return {
				url:"",
				islook:false,
				typelist: 0, //0为获取合作协议  1为获取保险单
				listdata: '', //列表数据
			}
		},

		onLoad: function(option) {
			// let that = this;
			// that.typelist = option.typelist || 0 //0为获取合作协议  1为获取保险单
			// if (that.typelist == 1) {
				uni.setNavigationBarTitle({
					title: "合同下载"
				})
			// }
				this.getWorkerContractList()
		},
		methods: {
			// 查看pdf
			look(e){
				this.url = e.url
				this.islook = true
				uni.navigateTo({
					url:`/pages/webview/index?url=${this.url}`
				})
			},
			/* 获取列表 */
			getWorkerContractList() {
				let that = this;
				// let data = {
				// 	type:this.typelist
				// }
				getWorkerContract().then(res => {
					that.listdata = res.data
					for (var i = 0; i < that.listdata.length; i++) {
						that.listdata[i].add_time = that.formatDate(that.listdata[i].add_time)
					}
					console.log('数据',that.listdata)
				})
			},

			// 时间戳处理
			formatDate: function(value, spe = '-') {
				value = value * 1000
				let data = new Date(value);
				let year = data.getFullYear();
				let month = data.getMonth() + 1;
				let day = data.getDate();
				let h = data.getHours();
				let mm = data.getMinutes();
				let s = data.getSeconds();
				month = month <  10 ? "0" + month : month;
					console.log('day',day)
				day = day > 10 || day == 10  ? day : "0" + day;
				h = h > 10 ? h : "0" + h;
				mm = mm > 10 ? mm : "0" + mm;
				s = s > 10 ? s : "0" + s;
			
				return `${year}${spe}${month}${spe}${day} ${h}:${mm}:${s}`;
			},


			/* 下载文件 */
			downWord: function(url) {
				console.log("url", url);
				uni.showModal({
					content: '是否下载？',
					success: function(res) {
						if (res.confirm) {

							uni.downloadFile({
								url,
								success: function(res) {
									//statusCode状态为200表示请求成功，tempFIlePath临时路径
									if (res.statusCode == 200) {
										console.log("ccc", res.tempFilePath);
										//保存到本地
										uni.saveFile({
											tempFilePath: res.tempFilePath,
											success: function(res) {
												//res.savedFilePath文件的保存路径
												//保存成功并打开文件
												uni.openDocument({
													filePath: res
														.savedFilePath,
													success: (res) =>
														console.log(
															'成功打开文档')
												})
												console.log("bbb", res);
											},
											fail() {
												console.log('打开失败');
												uni.showToast({
													title: '打开失败,请重新下载',
													icon: 'none',
												})
											}
										})
									}
									console.log("aaa", res);
								},
								fail() {
									console.log('下载失败');
									uni.showToast({
										title: '下载失败,请重新下载',
										icon: 'none',
									})

								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}



		}
	}
</script>

<style scoped lang="less">
	.warp {
		padding: 40rpx 30rpx;
		box-sizing: border-box;
		background: #FFFFFF;


		.htxzbox {
			background: #F6F9FF;
			box-shadow: 0px 0px 5px #E6EFFF;
			margin-bottom: 40rpx;
			padding: 32rpx 32rpx 28rpx 32rpx;
			box-sizing: border-box;

			.htxztime {
				font-size: 24rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: #999999;
				padding: 0 0 8rpx 0;
				border-bottom: 1rpx #EBF5FF solid;
			}

			.htxztitle {
				font-size: 36rpx;
				font-weight: 500;
				line-height: 42rpx;
				color: #333333;
				margin: 16rpx 0 14rpx 0;
			}

			.htxzbtn {
				display: block;
				text-align: right;

				.btnlook {
					font-size: 28rpx;
					font-weight: 400;
					line-height: 32rpx;
					color: #029E8E;
					display: inline-block;
				}

				.btndown {
					font-size: 28rpx;
					font-weight: 400;
					line-height: 32rpx;
					color: #FEC860;
					display: inline-block;
					margin: 0 0 0 60rpx;
				}
			}

		}
	}
	// .close{
	// 	position: fixed;
	// 	top: 30rpx;
		
	// 	right: 30rpx;
	// 	color: #007AFF;
	// }
</style>
